package com.renjianbo.project.module.system.controller;

import com.renjianbo.project.common.model.PageInfo;
import com.renjianbo.project.common.model.PageReq;
import com.renjianbo.project.common.util.JsonUtil;
import com.renjianbo.project.module.system.model.sys_permission.SysPermissionVO;
import com.renjianbo.project.module.system.model.sys_role.*;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.renjianbo.project.module.system.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import com.renjianbo.project.common.model.Rest;

import java.util.List;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 任建波
 * @since 2022-04-19
 */
@Api(tags = {""})
@RestController
@RequestMapping("sys-role")
public class SysRoleController {

    @Autowired
    private SysRoleService sysRoleService;

    @ApiOperation(value = "查询数据")
    @GetMapping("list")
    public Rest<List<SysRoleVO>> list(SysRoleSearchReq req){
        List<SysRoleDTO> list = sysRoleService.list(req);
        List<SysRoleVO> voList = JsonUtil.listTransform(list, SysRoleVO.class);

        return Rest.success(voList);
    }

    @ApiOperation(value = "新增")
    @PostMapping("add")
    public Rest<String> add(@RequestBody @Validated SysRoleAddReq req){
        sysRoleService.add(req);

        return Rest.success();
    }

    @ApiOperation(value = "删除")
    @ApiImplicitParams({
        @ApiImplicitParam(name="id",value="id",required=true),
    })
    @PostMapping("delete")
    public Rest<String> delete(@RequestParam Long id){
        sysRoleService.delete(id);

        return Rest.success();
    }

    @ApiOperation(value = "更新")
    @PostMapping("update")
    public Rest<String> update(@RequestBody @Validated SysRoleUpdateReq req){
        sysRoleService.update(req);

        return Rest.success();
    }

    @ApiOperation(value = "根据id查询")
    @ApiImplicitParams({
        @ApiImplicitParam(name="id",value="id",required=true),
    })
    @GetMapping("get-by-id")
    public Rest<SysRoleVO> getById(@RequestParam Long id){
        SysRoleDTO sysRoleDTO = sysRoleService.getById(id);
        SysRoleVO sysRoleVO = new SysRoleVO();
        BeanUtils.copyProperties(sysRoleDTO, sysRoleVO);

        return Rest.success(sysRoleVO);
    }

}
